#include "stdafx.h"
#include "UNonSpaceFillingContainerWindow.h"

TNonSpaceFillingContainerWindow*  TNonSpaceFillingContainerWindow::GetRoot()
{
	return this;
}

bool TNonSpaceFillingContainerWindow::IsFocussed()
{
	return true;
}

TNonSpaceFillingContainerWindow::TNonSpaceFillingContainerWindow(TRectangle Rect, TContainerWindow* parent, TWindowThemer *wt, std::string name)
	: TContainerWindow(Rect, parent, wt, name)
{
	DraggingWindow = NULL;
	Resizing= false;
}

void TNonSpaceFillingContainerWindow:: Draw( )
{

	for (unsigned int i=0; i < Children.size(); ++i)
	{
		Children[i]->Draw();
	}

	TDockable::Draw();
}

void TNonSpaceFillingContainerWindow::EventHandle(SDL_Event event)
{
	switch(event.type)
	{
		case(SDL_MOUSEMOTION):
		{
			MouseMove(event.motion.x, event.motion.y, event.motion.state);
			break;
		}
		case(SDL_MOUSEBUTTONDOWN):
		{
			MouseDown(event.motion.x, event.motion.y, event.button.button);
			break;
		}
		case(SDL_MOUSEBUTTONUP):
		{
			MouseUp(event.motion.x, event.motion.y, event.button.button);
			break;
		}
		case(SDL_KEYDOWN):
		{
			KeyDown(&event.key);
			break;
		}
		case(SDL_KEYUP):
		{
			KeyUp(&event.key);
			break;
		}
	}
}


void TNonSpaceFillingContainerWindow::WindowMove(int X, int Y)
{
	bool InOtherWindow = false;

	for (unsigned int i=Children.size(); i-- > 0; )
	{
		TDockable* Window = Children[i];
		if (Window->Contains(X, Y)) {
			if (Window == DraggingWindow)
				continue;
			if (Window == DraggingWindow->Parent)
				return;
			InOtherWindow = Window->Dock(DraggingWindow, X, Y);
			break;
		}
	}

	if (!InOtherWindow)
	{
		if (DraggingWindow->Parent != this) {
			DraggingWindow->Parent->RemoveChild(DraggingWindow);
			AddChild(DraggingWindow);
			DraggingWindow->Right = DraggingWindow->Left + DragWindowWidth;
			DraggingWindow->Bottom = DraggingWindow->Top + DragWindowHeight;
		}
		int Nx = X+DragWindowLeft;
		int Ny = Y+DragWindowTop;
		DraggingWindow->SetPos(TRectangle(Nx, Ny, Nx+DraggingWindow->Width(), Ny+DraggingWindow->Height()));
	}
}

void TNonSpaceFillingContainerWindow::MouseMove(int X, int Y, int mstate)
{
	if (DraggingWindow != NULL)
		WindowMove(X,Y);

	if (Resizing)
	{
		TDockable* ResizingWindow = Children[ActiveChildindex];
		ResizingWindow->SetPos(TRectangle(ResizingWindow->Left, ResizingWindow->Top, X, Y));
	}

	if (!DraggingWindow && !Resizing)
	{
		for (unsigned int i=Children.size(); i-- > 0; )
		{
			if (Children[i]->Contains(X, Y)	) {
				Children[i]->MouseMove(X, Y, mstate);
				break;
			}
		}

		if (ActiveChild()!=NULL && !ActiveChild()->Contains(X, Y))
		{
			ActiveChild()->MouseLeave();
			ActiveChild()->MouseMove(X, Y, mstate);
		}

	}

}

void TNonSpaceFillingContainerWindow:: MouseDown(int X, int Y, uint8 button)
{
	for (unsigned int i=Children.size(); i-- > 0; )
	{
		TDockable* Child = Children[i];
		if (Child->Contains(X, Y))
		{
			setActiveChild(i);

			if (Child->ResizeAtPos(X, Y))
			{
				Resizing = true;
			}
			else
			{
				Child->MouseDown(X, Y, button);
			}
			break;
		}
	}

	TContainerWindow::MouseDown(X, Y, button);
}

void TNonSpaceFillingContainerWindow::MouseUp(int X, int Y, uint8 button)
{
	TContainerWindow::MouseUp(X, Y, button);
	DraggingWindow = NULL;
	Resizing = false;
	if (ActiveChild()!=NULL && ActiveChild()->Contains(X, Y))
		ActiveChild()->MouseUp(X, Y, button);
}

void TNonSpaceFillingContainerWindow::setActiveChild(int Child)
{
	TDockable* node = Children[Child];
	Children.erase(Children.begin()+Child); // bit hacky...
	Children.push_back(node);
	TContainerWindow::setActiveChild(Children.size()-1);
}

int TNonSpaceFillingContainerWindow::getCursorType(int X, int Y)
{
	if	(DraggingWindow!=NULL)
		return 1;

	if (Resizing)
		return  2;

	for (unsigned int i=Children.size();i-- > 0;)
		if (Children[i]->Contains(X, Y))
			if (Children[i]->ResizeAtPos(X, Y))
				return  2;
	return 0; //Default/Error (Should we get here??)
}
